import { _decorator, Component, Node } from 'cc';
import UiManager from './UiManager';
import { perr, plog } from '../Utils/Tools';
const { ccclass, property } = _decorator;

@ccclass('ParamUI')
export class ParamUI extends Component {
    // Caller: any
    // FuncNomal: any
    // FuncMsgBox: any
    NodeBase: Node
    // FuncBase: any
    public CallbackEndBase: (pm) => void = null;//如果从最底层打卡的，保存关闭时的回调
    private MapCallbackOpen: Map<string, (pm) => void> = null;//打开此UI时的刷新，string:从哪个UI名字打开的
    private MapCallbackClose: Map<string, (pm) => void> = null;//关闭上层UI时的刷新，string:从哪个UI名字关闭的
    private isInit: boolean = false
    onLoad(): void {
        this.Init()
    }
    public Init() {
        if (this.isInit) {
            return
        }
        this.MapCallbackOpen = new Map();
        this.MapCallbackClose = new Map();
        this.RegisterCallbackOpen("-", (pm) => plog("callback:", this.node.name, " open default. pm:", pm))
        this.RegisterCallbackClose("-", (pm) => plog("callback:", this.node.name, " close default. pm:", pm))
        this.isInit = true
    }

    start() {

    }

    update(deltaTime: number) {

    }
    /**
    public SetCallbackRefresh(funcNomal) {
        // this.Caller = caller
        this.FuncNomal = funcNomal
    }
    public CallbackRefreshUI(pm: any) {//pm的type用来区分解析流程 0 为固定的msg box
        // if (this.Caller != null && pm != null) {
        if (pm != null) {
            switch (pm.type) {
                case 0:
                    {
                        if (this.FuncMsgBox != null) {
                            // this.FuncMsgBox.call(this.Caller, pm.ok)
                            this.FuncMsgBox(pm.ok)
                        } else if (this.FuncNomal != null) {
                            // this.FuncNomal.call(this.Caller, pm, pm.type)
                            this.FuncNomal(pm, pm.type)
                        }
                    }
                    break;
                default:
                    {
                        if (this.FuncNomal != null) {
                            // this.FuncNomal.call(this.Caller, pm, pm.type)
                            this.FuncNomal(pm, pm.type)
                        }
                    }
                    break;
            }
        }
    }
    public CallbackUiBase(pm: any) {
        if (pm != null && this.FuncBase != null) {
            if (pm.type == 0) {//msg box
                // this.FuncBase.call(this.Caller, pm.ok)
                this.FuncBase(pm.ok)
            } else {
                // this.FuncBase.call(this.Caller, pm)
                this.FuncBase(pm)
            }

        } else {
            perr("CallbackUiBase node:", this.node.name, " pm is null")
        }
    }
    public SetBaseNode(node: Node, callback: any) {
        this.NodeBase = node
        this.FuncBase = callback
    }
    public MsgBox(label: string, callback: (ok: boolean) => void) {
        if (callback === null) {
            perr("msg box: ", this.node.name, "has not callback")
            return
        }
        this.FuncMsgBox = callback
        UiManager.Instance.AddUI(this.node, 'msg_box', { label: label })
    }
    public Hint(label: string, pm: string = "") {
        UiManager.Instance.AddUI(this.node, 'hint', { label: label, param: pm })
    }
    /**/


    public Hint(label: string, callback: (pm) => void = null) {
        // plog("hint label 1.", label)
        const name = 'hint'
        if (callback !== null) {
            this.RegisterCallbackClose(name, callback)
            UiManager.Instance.AddUI(this.node, 'hint', { label: label, param: name })
        } else {
            UiManager.Instance.AddUI(this.node, 'hint', { label: label, param: null })
        }

    }
    public MsgBox(label: string, callback: (ok: boolean) => void) {
        if (callback === null) {
            perr("msg box: ", this.node.name, "has not callback")
            return
        }
        const name = 'msg_box'
        this.RegisterCallbackClose(name, callback)
        UiManager.Instance.AddUI(this.node, name, { label: label })
    }

    public TransmitCallback(puOld: ParamUI) {
        this.NodeBase = puOld.NodeBase
        this.CallbackEndBase = puOld.CallbackEndBase
    }
    public RegisterBaseCallback(nodeBase: Node, func: (pm) => void) {
        this.NodeBase = nodeBase
        this.CallbackEndBase = func
    }
    public RegisterCallbackOpen(ui: string, func: (pm) => void) {
        this.MapCallbackOpen.set(ui, func)
    }
    public RegisterCallbackClose(ui: string, func: (pm) => void) {
        this.MapCallbackClose.set(ui, func)
    }

    //最底层ui用base，通用层ui用 -
    public CallFuncOpen(ui: string, pm: any) {
        if (pm === null) {
            return
        }
        if (this.MapCallbackOpen.has(ui)) {
            let func = this.MapCallbackOpen.get(ui)
            func(pm)
        } else {
            let func = this.MapCallbackOpen.get('-')
            func(pm)
        }

        // else if (this.MapCallbackOpen.has('-')) {
        //     let func = this.MapCallbackOpen.get('-')
        //     func(pm)
        // } else {
        //     perr("call func :", this.node.name, " has not key:", ui)
        // }
    }

    //最底层ui用base，通用层ui用 -
    public CallFuncClose(ui: string, pm: any) {
        if (pm === null) {
            return
        }
        if (this.MapCallbackClose.has(ui)) {
            let func = this.MapCallbackClose.get(ui)
            func(pm)
        } else {
            let func = this.MapCallbackClose.get('-')
            func(pm)
        }

        // else if (this.MapCallbackClose.has('-')) {
        //     let func = this.MapCallbackClose.get('-')
        //     func(pm)
        // } else {
        //     perr("call func :", this.node.name, " has not key:", ui)
        // }
    }




}


